$(function () {
    $("#jqGrid").jqGrid({
        url: '../emparchivesbase/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true ,hidden: true},
			{ label: '在职档案编号', name: 'jobArchivesNo', index: 'job_archives_no', width: 120 },
            { label: '离职档案编号', name: 'quitArchivesNo', index: 'quit_archives_no', width: 120 },
            { label: '单位id', name: 'settlementId', index: 'settlement_id', width: 80 },
            { label: '单位名称', name: 'settlementName', index: 'settlement_name', width: 80 },
			{ label: '员工编号', name: 'empNo', index: 'emp_no', width: 80 }, 			
			{ label: '员工姓名', name: 'empName', index: 'emp_name', width: 80 }, 			
			{ label: '求职登记表', name: 'jobRegForm', index: 'job_reg_form', width: 80,formatter:getValue,unformat:ungetValue },
			{ label: '身份证复印件', name: 'cardidCopy', index: 'cardid_copy', width: 80 ,formatter:getValue,unformat:ungetValue},
			{ label: '相片', name: 'photo', index: 'photo', width: 80 ,formatter:getValue,unformat:ungetValue},
			{ label: '劳动合同', name: 'laborContract', index: 'labor_contract', width: 80 ,formatter:getValue,unformat:ungetValue},
			{ label: '体检表', name: 'physicalExam', index: 'physical_exam', width: 80,formatter:getValue,unformat:ungetValue },
			{ label: '员工手册签收单', name: 'empManual', index: 'emp_manual', width: 80 ,formatter:getValue,unformat:ungetValue},
			{ label: '解除合同证明书', name: 'dimissionCertification', index: 'dimission_certification', width: 80 ,formatter:getdimission,unformat:ungetdimission,hidden: true},
            { label: '档案状态', name: 'checkState', index: 'check_state', width: 80,formatter:getcheckState ,unformat:ungetcheckState},
            { label: '备注', name: 'remark', index: 'remark', width: 80 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    vm.archiveRecord();
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        q: {
            name: null,

        },
		showList: true,
        showTable: true,
		title: null,
		empArchivesbase: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.empArchivesbase = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.empArchivesbase.id == null ? "../emparchivesbase/save" : "../emparchivesbase/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.empArchivesbase),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}

			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../emparchivesbase/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../emparchivesbase/info/"+id, function(r){
                vm.empArchivesbase = r.empArchivesbase;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'emp_name': vm.q.name},
                page:page
            }).trigger("reloadGrid");
		},
        archiveRecord: function (id) {
            $("#jqGridSub").jqGrid({
                url: '../emparchivesbase/archiveRecord',
                datatype: "local",
                colModel: [
                    { label: 'id', name: 'id', index: 'id', width: 50, key: true },
                    { label: '档案表id', name: 'empArchivesbaseId', index: 'emp_archivesbase_id', width: 80 },
                    { label: '档案类型(例:户口本)', name: 'archivesbaseCategory', index: 'archivesbase_category', width: 80 },
                    { label: '操作()', name: 'operation', index: 'operation', width: 80 },
                    { label: '操作人', name: 'operator', index: 'operator', width: 80 },
                    { label: '操作日期', name: 'operatorDate', index: 'operator_date', width: 80 },
                    { label: '审核状态', name: 'checkState', index: 'check_state', width: 80 }
                ],
                viewrecords: true,
                height: 385,
                rowNum: 10,
                rowList : [10,30,50],
                rownumbers: true,
                rownumWidth: 25,
                autowidth:true,
                multiselect: true,
                pager: "#jqGridPagerSub",
                jsonReader : {
                    root: "page.list",
                    page: "page.currPage",
                    total: "page.totalPage",
                    records: "page.totalCount"
                },
                prmNames : {
                    page:"page",
                    rows:"limit",
                    order: "order"
                },
                gridComplete:function(){
                    //隐藏grid底部滚动条
                    $("#jqGridSub").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
                }
            });
        },
        reloadjqGridSub: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            vm.showTable = false;
            var page = $("#jqGridSub").jqGrid('getGridParam','page');
            $("#jqGridSub").jqGrid('setGridParam',{
                datatype: "json",
                postData:{'emp_archivesbase_id': id},
                page:page
            }).trigger("reloadGrid");
        },
        archivePass:function(){
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            vm.archive(ids,'1');
        },
        archiveNoPass:function(){
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            vm.archive(ids,'9');
        },
        probationLeave:function(){
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            vm.archive(ids,'8');
        },
        archive:function (ids,status) {

            confirm('确定选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: "../emparchivesbase/archivePass?status="+status,
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(index){
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        archiveChangePass:function (event) {
            var id = vm.getRow();
            if(!id){
                return ;
            }
        confirm('确定选中的记录？', function(){
            $.ajax({
                type: "POST",
                url: "../emparchivesbase/archiveChangePass",
                contentType: "application/json",
                data: JSON.stringify(id),
                success: function(r){
                    if(r.code == 0){
                        alert('操作成功', function(index){
                            $("#jqGridSub").trigger("reloadGrid");
                        });
                    }else{
                        alert(r.msg);
                    }
                }
             });
         });
        },
        certificateAcceptance:function(){
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            vm.acceptance(ids,'1');
        },
        acceptance:function (ids,status) {
            confirm('确定选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: "../emparchivesbase/acceptance?status="+status,
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(index){
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getRows: function(){
            var grid = $("#jqGridSub");
            var rowKey = grid.getGridParam("selrow");
            if(!rowKey){
                alert("请选择一条记录");
                return ;
            }

            var ids = grid.getGridParam("selarrrow");
            return ids;

        },
        getRow: function() {
            var grid = $("#jqGridSub");
            var rowKey = grid.getGridParam("selrow");
            if(!rowKey){
                alert("请选择一条记录");
                return ;
            }

            var selectedIDs = grid.getGridParam("selarrrow");
            if(selectedIDs.length > 1){
                alert("只能选择一条记录");
                return ;
            }

             return selectedIDs[0];
        },
        archiveChangeNoPass: function (event) {
            var ids = vm.getRows();
            if(ids == null){
                return ;
            }
            confirm('确定要退回选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: "../emparchivesbaserecord/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(index){
                                $("#jqGridSub").trigger("reloadGrid");
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },





	}
});
function getValue(cellvalue, options, rowObject){
    var html="";

    if(cellvalue=="0"){
        html="不需要";
    }else if(cellvalue=="1"){
        html="需要";
    }
    return html;
}
function ungetValue(cellvalue, options, rowObject){
    var html="";

    if(cellvalue=="不需要"){
        html="0";
    }else if(cellvalue=="需要"){
        html="1";
    }
    return html;
}

function getcheckState(cellvalue, options, rowObject){
    var html="";

    if(cellvalue=="0"){
        html="未审核";
    }else if(cellvalue=="1"){
        html="已审核";
    }else if(cellvalue=="8"){
        html="试用期离职";
    }else if(cellvalue=="9"){
        html="已退回";
    }
    return html;
}
function ungetcheckState(cellvalue, options, rowObject){
    var html="";

    if(cellvalue=="未审核"){
        html="0";
    }else if(cellvalue=="已审核"){
        html="1";
    }else if(cellvalue=="试用期离职"){
        html="8";
    }else if(cellvalue=="已退回"){
        html="9";
    }
    return html;
}
function getdimission(cellvalue, options, rowObject){
    var html="";

    if(cellvalue=="0"){
        html="未接收";
    }else if(cellvalue=="1"){
        html="已接收";
    }
    return html;
}
function ungetdimission(cellvalue, options, rowObject){
    var html="";

    if(cellvalue=="未接收"){
        html="0";
    }else if(cellvalue=="已接收"){
        html="1";
    }
    return html;
}